package com.example.myapplicationndk.expand;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.example.myapplicationndk.R;
import com.example.myapplicationndk.databinding.ActivityExpandMainBinding;
import com.example.myapplicationndk.utils.GenerateString;
import com.example.myapplicationndk.utils.Logd;

import java.util.ArrayList;
import java.util.List;

// 参见boottomsheetdemo
public class ExpandMainActivity extends AppCompatActivity {

    ActivityExpandMainBinding mBinding;
    private ListView list;
    private ItemAdapter adapter;
    private List<String> allCountres = new ArrayList<>();
    private int isTouchType = -1; // 0 mask  ;1-tvdrag
    private double mLastx = 0;
    private double mLastY = 0;
    private BottomSheetBehavior<LinearLayout> mBehavior;
    private List<Item> list1;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        mBinding = ActivityExpandMainBinding.inflate(getLayoutInflater());
        setContentView(mBinding.getRoot());


        mBehavior = BottomSheetBehavior.from(mBinding.bottomSheet);

        mBinding.recyclerview.setLayoutManager(new LinearLayoutManager(this));
        mBinding.recyclerview.setHasFixedSize(true);

        list1 = createItems();
        adapter = new ItemAdapter(list1, new ItemAdapter.OnItemClickListener() {
            @Override
            public void onclick(View v, int pos) {
                Logd.Logd(list1.get(pos).getTitle() + " " + pos);
            }
        });
        mBinding.recyclerview.setAdapter(adapter);

        mBehavior.addBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View bottomSheet, int newState) {

            }

            @Override
            public void onSlide(@NonNull View bottomSheet, float slideOffset) {

            }
        });

        SoftKeyBoardListener.setListener(ExpandMainActivity.this, new SoftKeyBoardListener.OnSoftKeyBoardChangeListener() {

                    @Override
                    public void keyBoardShow(int height) {
//                        mBehavior.setState(BottomSheetBehavior.STATE_INIT);
                    }

                    @Override
                    public void keyBoardHide(int height) {
                        try {
                            mBehavior.setState(BottomSheetBehavior.STATE_INIT);
//                            mBehavior.onLayoutChild(mBinding.llCor, mBinding.bottomSheet, LayoutDirection.LTR);
                        } catch (Exception e) {
                            Logd.Logd(e.toString());
                        }

                    }
                }
        );
    }


    public List<Item> createItems() {
        ArrayList<Item> items = new ArrayList<>();
        for (int i = 0; i < 50; i++) {
            items.add(new Item(R.mipmap.ic_fengchao, GenerateString.getRandomString((5 + i) % 8)));
        }
        items.add(new Item(R.mipmap.ic_fengchao, "哈哈"));
        items.add(new Item(R.mipmap.ic_fengchao, "爱你"));
        items.add(new Item(R.mipmap.ic_fengchao, "汗"));
        items.add(new Item(R.mipmap.ic_fengchao, "晕了"));
        items.add(new Item(R.mipmap.ic_fengchao, "无语"));
        items.add(new Item(R.mipmap.ic_fengchao, "呵呵"));
        items.add(new Item(R.mipmap.ic_fengchao, "日"));
        return items;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }
}